package com.jushu.storbox.adapter;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.jushu.storbox.R;

public class FileBrowserDocumentAdapter extends BaseAdapter {
	private Context context;

	private List<File> files;

	private ViewHoder holder;

	private Map<String, Boolean> checkBoxState;

	private ArrayList<String> selectedFiles = null;

	public ArrayList<String> getSelectedFiles() {
		return selectedFiles;
	}

	public ViewHoder getHolder() {
		return holder;
	}

	public Map<String, Boolean> getCheckBoxState() {
		return checkBoxState;
	}

	public FileBrowserDocumentAdapter(List<File> files, Context context) {
		this.files = files;
		this.context = context;
		selectedFiles = new ArrayList<String>();
		initCacheState(files);
	}

	@Override
	public int getCount() {
		return files.size();
	}

	@Override
	public File getItem(int position) {
		return files.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@SuppressLint("SimpleDateFormat")
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		final File item = files.get(position);

		if (null != checkBoxState && checkBoxState.size() == 0) {
			initCacheState(files);
		}
		if (null == convertView) {
			convertView = LayoutInflater.from(context).inflate(R.layout.item_file_browse, null);
			holder = new ViewHoder();
			holder.iv = (ImageView) convertView.findViewById(R.id.iv_icon);
			holder.tv_name = (TextView) convertView.findViewById(R.id.tv_name);
			holder.tv_detail = (TextView) convertView.findViewById(R.id.tv_detail);
			holder.checkBox = (CheckBox) convertView.findViewById(R.id.is_item_select);
			holder.checkBox.setTag(item);
			convertView.setTag(holder);
		} else {
			holder = (ViewHoder) convertView.getTag();
		}

		if (!item.isDirectory()) {
			holder.iv.setImageResource(R.mipmap.default_file_icon);
		} else {
			holder.iv.setImageResource(R.mipmap.folder_im);
		}
		holder.tv_name.setText(item.getName());
		holder.tv_detail.setText(new SimpleDateFormat("yyyy-MM-dd").format(new Date(item.lastModified())));

		holder.checkBox.setTag(item);
		holder.checkBox.setChecked(checkBoxState.get(item.toString()));

		holder.checkBox.setOnClickListener(new OnClickListenerImpl(item));
		return convertView;
	}

	public class ViewHoder {
		public ImageView iv;

		public TextView tv_name;

		public TextView tv_detail;

		public CheckBox checkBox;
	}

	private void initCacheState(List<File> files) {
		if (null == checkBoxState) {
			checkBoxState = new HashMap<String, Boolean>();
		}
		for (File file : files) {
			checkBoxState.put(file.toString(), false);
		}
	}

	public void unCheckBox() {
		if (null != holder) {
			holder.checkBox.setChecked(false);
		}
	}

	public void clearCacheState() {
		if (null != checkBoxState) {
			checkBoxState.clear();
		}
	}

	private class OnClickListenerImpl implements OnClickListener {
		private File file;

		public OnClickListenerImpl(File file) {
			this.file = file;
		}

		@Override
		public void onClick(View view) {
			if (((CheckBox) view).isChecked()) {
				checkBoxState.put(file.toString(), true);
				selectedFiles.add(file.getAbsolutePath());
			} else {
				checkBoxState.put(file.toString(), false);
				selectedFiles.remove(file.getAbsolutePath());
			}
		}

	}
}
